package top.goldenyear.porpoise.infra.job.service;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.easy.query.solon.annotation.Db;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import top.goldenyear.porpoise.common.core.exception.ErrorCode;
import top.goldenyear.porpoise.common.core.exception.ServiceException;
import top.goldenyear.porpoise.common.core.model.PlainOption;
import top.goldenyear.porpoise.framework.web.core.model.PageResult;
import top.goldenyear.porpoise.framework.web.core.model.query.QPage;
import top.goldenyear.porpoise.framework.web.core.service.BaseService;
import top.goldenyear.porpoise.infra.job.model.JobLog;
import top.goldenyear.porpoise.infra.job.model.proxy.JobLogProxy;

/**
 * 定时任务日志表 Service 服务层
 *
 * @author airhead
 * @since 2024-12-21
 */
@Component
@Slf4j
public class JobLogService extends BaseService<JobLog, JobLogProxy> {
  @Db private EasyEntityQuery easyEntityQuery;

  public JobLogService() {
    super(JobLog.class, JobLogProxy.class);
  }

  public Boolean createJobLog(JobLog jobLog) {
    return save(jobLog);
  }

  public Boolean updateJobLog(JobLog jobLog) {
    return updateById(jobLog);
  }

  public JobLog getJobLog(Long id) {
    return getById(id);
  }

  public Boolean deleteJobLog(Long id) {
    if (getById(id) == null) {
      throw new ServiceException(ErrorCode.builder().msg("记录不存在").build());
    }

    return deleteById(id);
  }

  public Boolean batchDelJobLog(List<Long> ids) {
    for (Long id : ids) {
      if (getById(id) == null) {
        throw new ServiceException(ErrorCode.builder().msg("记录不存在").build());
      }
    }

    return deleteByIds(ids);
  }

  public List<JobLog> listJobLog(JobLog jobLog) {
    return list(jobLog);
  }

  public List<JobLog> simpleListJobLog(JobLog jobLog) {
    return list(jobLog);
  }

  public PageResult<JobLog> paginateJobLog(QPage qPage, JobLog jobLog) {
    return paginate(qPage, jobLog);
  }

  public List<PlainOption> optionsJobLog(JobLog jobLog) {
    return listAs(jobLog, PlainOption.class);
  }

  @Override
  protected EasyEntityQuery easyEntityQuery() {
    return easyEntityQuery;
  }
}
